//
//  YQLoginView.m
//  OverseasImmigration
//
//  Created by yuqin on 2019/5/21.
//  Copyright © 2019年 yuqin. All rights reserved.
//

#import "YQLoginView.h"

@interface YQLoginView ()

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation YQLoginView
{
    NSInteger _count;
    NSString *_teleNum;
    NSString *_codeNum;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

- (void)setupUI {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"X" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    backBtn.contentMode = UIViewContentModeLeft;
    [self addSubview:backBtn];
    self.quitBtn = backBtn;
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(@(FIT(16)));
        make.top.mas_offset(@(FIT(20)));
        make.width.height.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    UILabel *describeLab = [[UILabel alloc] init];
    describeLab.text = @"手机号快捷登录";
    describeLab.textColor = [UIColor blackColor];
    describeLab.font = YQFont(20);
    [self addSubview:describeLab];
    [describeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(FIT(100));
        make.centerX.equalTo(self.mas_centerX);
    }];
    
    self.telephoneNum = [[UITextField alloc] init];
    self.telephoneNum.borderStyle = UITextBorderStyleNone;
    self.telephoneNum.placeholder = @"请输入手机号";
    self.telephoneNum.font = Font_16;
    self.telephoneNum.tag = 10;
    self.telephoneNum.keyboardType = UIKeyboardTypeNumberPad;
    [self addSubview:self.telephoneNum];
    [self.telephoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(FIT(32));
        make.right.mas_equalTo(FIT(-32));
        make.height.mas_equalTo(FIT(30));
        make.top.equalTo(describeLab.mas_bottom).mas_equalTo(FIT(80));
    }];
    [self.telephoneNum addTarget:self action:@selector(textEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
    self.codeTF = [[UITextField alloc] init];
    self.codeTF.borderStyle = UITextBorderStyleNone;
    self.codeTF.placeholder = @"请输入验证码";
    self.codeTF.font = Font_16;
    self.codeTF.tag = 11;
    [self addSubview:self.codeTF];
    [self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(FIT(32));
        make.height.mas_equalTo(FIT(30));
        make.top.equalTo(self.telephoneNum.mas_bottom).mas_equalTo(FIT(30));
        make.width.mas_equalTo(SCREENWIDTH/3.0*2-32);
    }];
    [self.codeTF addTarget:self action:@selector(textEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
    self.loadCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loadCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.loadCodeBtn setTitleColor:UIColorFromRGB(0x6b3a7) forState:UIControlStateNormal];
    self.loadCodeBtn.titleLabel.font = YQFont(14);
    [self addSubview:self.loadCodeBtn];
    [self.loadCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.codeTF.mas_right).mas_equalTo(0);
        make.top.equalTo(self.telephoneNum.mas_bottom).mas_equalTo(FIT(30));
        make.right.mas_equalTo(-32);
        make.height.mas_equalTo(FIT(30));
        
    }];
    [self.loadCodeBtn addTarget:self action:@selector(loadCodeAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitBtn setTitle:@"登录" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commitBtn setBackgroundColor:UIColorFromRGB(0x6b3a7)];
    commitBtn.titleLabel.font = YQFont(14);
    commitBtn.layer.cornerRadius = FIT(20);
    [self addSubview:commitBtn];
    self.commitBtn = commitBtn;
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(FIT(32));
        make.right.mas_equalTo(FIT(-32));
        make.height.mas_equalTo(FIT(40));
        make.top.equalTo(self.codeTF.mas_bottom).mas_equalTo(FIT(40));
    }];
    //登录
//    __weak typeof(self) weakSelf = self;
    [[commitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        //号码判断
        if (isEmptyString(_teleNum)) {
            //电话号码没填
            [LCProgressHUD showMessage:@"请输入电话号码"];
            return;
        }
        
        if ([LimitTextFieldNum validateCellPhoneNumber:_teleNum] == NO) {
            [LCProgressHUD showMessage:@"请输入正确的电话号码"];
            return;
        }
        //验证码判断是否为空
        if (isEmptyString(_codeNum)) {
            [LCProgressHUD showMessage:@"请输入验证码"];
            return ;
        }
        if (self.block) {
            self.block(_teleNum, _codeNum);
        }
    }];
    
    UILabel *desLab = [[UILabel alloc] init];
    desLab.text = @"未注册的手机号登录时将自动注册";
    desLab.font = YQFont(13);
    desLab.textAlignment = NSTextAlignmentCenter;
    desLab.textColor = [UIColor lightGrayColor];
    [self addSubview:desLab];
    [desLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(commitBtn.mas_bottom).mas_equalTo(FIT(10));
    }];
}


- (void)loadCodeAction {
    self.loadCodeBtn.enabled = NO;
    //点击之前判断电话号码是否输入已经输入正确
    if (isEmptyString(_teleNum)) {
        //电话号码没填
        [LCProgressHUD showMessage:@"请输入电话号码"];
        return;
    }
    
    if ([LimitTextFieldNum validateCellPhoneNumber:_teleNum] == NO) {
        [LCProgressHUD showMessage:@"请输入正确的电话号码"];
        return;
    }
    //数据请求
    NSString *code = [NSString stringWithFormat:@"%@user/senNotify",BaseHttpUrl];
    [[HTTPRequstManger sharedHTTPRequstManger] getRequestWithUrlString:code appendParameters:@{@"phone":_teleNum} successBlock:^(id responseObject) {
        
    } failureBlock:^(NSError *error) {
        
    }];
    
    _count = 30;
    //倒计时30秒
    [self.loadCodeBtn setTitle:[NSString stringWithFormat:@"%ld",_count] forState:UIControlStateNormal];
    __weak typeof(self) weakSelf = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakSelf timerRun];
    }];
}

#pragma mark - 倒计时
- (void)timerRun {
    _count --;
    [self.loadCodeBtn setTitle:[NSString stringWithFormat:@"%ld",_count] forState:UIControlStateNormal];
    if (_count == 0) {
        self.loadCodeBtn.enabled = YES;
        [self.loadCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        
    }
    
}

- (void)textEditChanged:(UITextField *)textField {
    NSLog(@"%@",textField.text);
    if (textField.tag == 10) {
        _teleNum = textField.text;
        [LimitTextFieldNum limitTextFieldContentNumWith:textField withMaxNum:11];
//        if (self.teleBlock) {
//            self.teleBlock(textField.text);
//        }
    }else {
        _codeNum = textField.text;
        [LimitTextFieldNum limitTextFieldContentNumWith:textField withMaxNum:6];
//        if (self.block) {
//            self.block(textField.text);
//        }
    }
    
}
@end
